package vn.appmobi.comictp;

import android.content.Context;
import android.content.SharedPreferences;
import vn.gq.tym.model.item.ChapterItem;

public class Bookmark {
	
	public static final String PREF_CHAPTER_KEY = "pref_chapter";
	public static final String PREF_POSITION_KEY = "pref_position";
	
	private static String prefName = "comic_bookmark";
	private static int prefMode = Context.MODE_PRIVATE;
	
	
	public static void saveChapter(Context context, ChapterItem chapterItem) {
		SharedPreferences sharepref = context.getSharedPreferences(prefName, prefMode);
		sharepref.edit()
			.putString(PREF_CHAPTER_KEY, chapterItem.toJsonObject().toString())
			.putInt(PREF_POSITION_KEY, 0)
			.commit();
	}
	
	public static void savePosition(Context context, int position) {
		SharedPreferences sharepref = context.getSharedPreferences(prefName, prefMode);
		sharepref.edit()
			.putInt(PREF_POSITION_KEY, position)
			.commit();
	}
	
	public static ChapterItem loadChapter(Context context) {
		SharedPreferences sharepref = context.getSharedPreferences(prefName, prefMode);
		String json = sharepref.getString(PREF_CHAPTER_KEY, null);
		if (json != null) {
			return new ChapterItem(json);
		}
		return null;
	}
	
	public static int getLastPosition(Context context) {
		SharedPreferences sharepref = context.getSharedPreferences(prefName, prefMode);
		return sharepref.getInt(PREF_POSITION_KEY, 0);
	}
}
